package com.animation_study.span;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.style.ReplacementSpan;

/**
 * Created by css on 2017/12/6.
 */

public class ColorSpan extends ReplacementSpan {
    Paint mPaint = new Paint();
    int mWidth;

    @Override
    public int getSize(@NonNull Paint paint, CharSequence charSequence, int i, int i1, @Nullable Paint.FontMetricsInt fontMetricsInt) {
        mWidth = (int) paint.measureText(charSequence, i, i1);
        return mWidth;
    }

    /**
     * canvas：用来绘制的画布；
     * <p>
     * text：整个text；
     * <p>
     * start：这个Span起始字符在text中的位置；
     * <p>
     * end：这个Span结束字符在text中的位置；
     * <p>
     * x：这个Span的其实水平坐标；
     * <p>
     * y：这个Span的baseline的垂直坐标；
     * <p>
     * top：这个Span的起始垂直坐标；
     * <p>
     * bottom：这个Span的结束垂直坐标；
     * <p>
     * paint：画笔
     *
     * @param canvas
     * @param text
     * @param start
     * @param end
     * @param x
     * @param top
     * @param y
     * @param bottom
     * @param paint
     */
    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        //draw the frame with custom Paint
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(x, top, x + mWidth, bottom, mPaint);
        canvas.drawText(text, start, end, x, y, paint);
    }
}
